﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Lswweb.IDAL;
using Lswweb.Model;

namespace Lswweb.SqlServerDAL
{
    public class ProductStorageDAL : IProductStorage
    {
        private Builder<ProductStorageInfo> builder = new Builder<ProductStorageInfo>("ProductStorages", "Id");

        public ProductStorageDAL()
        {
            this.builder.CreateModel += new ReaderBind<ProductStorageInfo>(builder_CreateModel);
            this.builder.GetParameters += new ParametersBuilder<ProductStorageInfo>(builder_GetParameters);
        }

        #region CreateModel、GetParameters

        IDataParameter[] builder_GetParameters(ProductStorageInfo t)
        {
            return new IDataParameter[]{
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@ProductName",t.ProductName),
                DataAccess.CreateParameter("@BrandName",t.BrandName),
                DataAccess.CreateParameter("@Model",t.Model),
                DataAccess.CreateParameter("@BatchNo",t.BatchNo),
                DataAccess.CreateParameter("@Quantity",t.Quantity),
                DataAccess.CreateParameter("@Remark",t.Remark),
                DataAccess.CreateParameter("@DateCreated",t.DateCreated),
                DataAccess.CreateParameter("@Ver",t.Ver)};
        }

        ProductStorageInfo builder_CreateModel(Hashtable reader)
        {
            ProductStorageInfo info=new ProductStorageInfo();

            if (reader.Contains("Id"))
                info.Id = Convert.ToInt32(reader["Id"]);

            if (reader.Contains("Model"))
                info.Model = reader["Model"].ToString();

            if (reader.Contains("BatchNo"))
                info.BatchNo = reader["BatchNo"].ToString();

            if (reader.Contains("Quantity"))
                info.Quantity = Convert.ToInt32(reader["Quantity"]);

            if (reader.Contains("Remark"))
                info.Remark = reader["Remark"].ToString();

            if (reader.Contains("DateCreated"))
                info.DateCreated =Convert.ToDateTime(reader["DateCreated"]);

            if (reader.Contains("Ver"))
                info.Ver = reader["Ver"].ToString();

            if (reader.Contains("IsHot"))
                info.IsHot = Convert.ToBoolean(reader["IsHot"]);

            if (reader.Contains("IsTop"))
                info.IsTop = Convert.ToBoolean(reader["IsTop"]);

            if (reader.Contains("ProductName"))
                info.ProductName = reader["ProductName"].ToString();

            if (reader.Contains("BrandName"))
                info.BrandName = reader["BrandName"].ToString();

            return info;
        }

        #endregion

        #region IProductStorage

        public int Insert(ProductStorageInfo model)
        {
            return this.builder.Insert(
                model,
                "BrandName", 
                "ProductName", 
                "Model", 
                "BatchNo", 
                "Quantity", 
                "Remark",
                "Ver");
        }

        public bool Update(ProductStorageInfo model)
        {
            return this.builder.Update(
                model,
                "Id=@Id",
                "BrandName",
                "ProductName",
                "Model",
                "BatchNo",
                "Quantity",
                "Remark");
        }

        public bool Delete(int Id)
        {
            return this.builder.Delete(Id);
        }

        public ProductStorageInfo GetProductStorage(int Id)
        {
            return this.builder.GetModel(Id);
        }

        public IList<ProductStorageInfo> GetProductStorages(string searchKey , string productName, string brandName,bool? isTop,bool? isHot, string ver, int pageIndex, int pageSize, out int totalRecord)
        {
            StringBuilder where = new StringBuilder();
            IList<IDataParameter> parameters = new List<IDataParameter>();

            if (!string.IsNullOrEmpty(searchKey))
            {
                where.Append(" and ProductStorages.Model like '%' + @SearchKey + '%'");
                parameters.Add(DataAccess.CreateParameter("SearchKey", searchKey));
            }

            if (!string.IsNullOrEmpty(productName))
            {
                where.Append(" and ProductStorages.ProductName like '%' + @ProductName + '%'");
                parameters.Add(DataAccess.CreateParameter("ProductName", productName));
            }

            if (!string.IsNullOrEmpty(brandName))
            {
                where.Append(" and ProductStorages.BrandName like '%' + @BrandName + '%'");
                parameters.Add(DataAccess.CreateParameter("BrandName",brandName));
            }

            //IsTop条件
            if (isTop != null)
            {
                parameters.Add(DataAccess.CreateParameter("@IsTop", isTop));
                where.Append(" and ProductStorages.IsTop=@IsTop");
            }

            //IsHot条件
            if (isHot != null)
            {
                parameters.Add(DataAccess.CreateParameter("@IsHot", isHot));
                where.Append(" and ProductStorages.IsHot=@IsHot");
            }

            if (!string.IsNullOrEmpty(ver))
            {
                parameters.Add(DataAccess.CreateParameter("Ver", ver));
                where.Append(" and ProductStorages.Ver=@Ver");
            }

            if (!string.IsNullOrEmpty(where.ToString()))
                where = where.Remove(0, 5);

            IDataParameter[] list = new IDataParameter[parameters.Count];
            parameters.CopyTo(list, 0);

            return this.builder.GetModels(string.Empty, where.ToString(), "ProductStorages.Id", pageIndex, pageSize, out totalRecord, list);
        }

        public bool UpdateIsTop(int id, bool isTop)
        {
            return builder.UpdateFields("IsTop", id, isTop);
        }

        public bool UpdateIsHot(int id, bool isHot)
        {
            return builder.UpdateFields("IsHot", id, isHot);
        }
        #endregion
    }
}
